package com.qianfeng.es.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.es.service.DocService;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Created by Jackiechan on 2022/9/7 21:49
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Service
public class DocServiceImpl implements DocService {

    private RestHighLevelClient restHighLevelClient;

    @Autowired
    public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    @Override
    public void addDoc(String index, String jsonData) throws Exception {
        IndexResponse response = restHighLevelClient.index(new IndexRequest(index).source(jsonData, XContentType.JSON), RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }

    @Override
    public void addDoc(String index, String id, String jsonData) throws IOException, Exception {
        IndexResponse response = restHighLevelClient.index(new IndexRequest(index).id(id).source(jsonData, XContentType.JSON), RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }

    @Override
    public void getDoc(String index, String id) throws Exception {
        GetResponse response = restHighLevelClient.get(new GetRequest(index, id), RequestOptions.DEFAULT);
        System.err.println(response.getSourceAsString());
    }

    @Override
    public void updateDoc(String index, String id, String json) throws Exception {
        UpdateResponse response = restHighLevelClient.update(new UpdateRequest(index, id).doc(json, XContentType.JSON), RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }

    @Override
    public void deleteDoc(String index, String id) throws Exception {
        DeleteResponse response = restHighLevelClient.delete(new DeleteRequest(index, id), RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }

    @Override
    public void deleteByQuery(String index, QueryBuilder queryBuilder) throws IOException {
        BulkByScrollResponse response = restHighLevelClient.deleteByQuery(new DeleteByQueryRequest(index).setQuery(queryBuilder), RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }
}
